package com.harmontronics.hanma.paas.paas.domain.tenant.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.consts.NumberEnum;
import com.harmontronics.hanma.paas.paas.domain.tenant.consts.HmTenantFields;
import com.harmontronics.hanma.paas.paas.domain.tenant.repository.persistence.HmTenantRepository;
import com.harmontronics.hanma.paas.paas.domain.tenant.entity.HmTenant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmontronics.hanma.paas.paas.common.PageBase;

import com.harmontronics.hanma.paas.paas.domain.tenant.repository.mapper.HmTenantMapper;
import com.harmontronics.hanma.paas.paas.domain.tenant.repository.po.HmTenantPO;
import java.util.List;

@Service("hmTenantService")
public class HmTenantService{

    @Autowired
    private HmTenantRepository hmTenantRepository;

    @Cacheable(value = "tenant",key = "#p0")
    public HmTenant findById( Long id){
            HmTenantPO po =   hmTenantRepository.getById(id);
        return BeanUtil.toBean(po,HmTenant.class);
    }

    public void save(HmTenant hmTenant){
            HmTenantPO po =  BeanUtil.toBean(hmTenant,HmTenantPO.class);
            hmTenantRepository.save(po);
    }

    @CacheEvict(value = "tenant",key = "#p0.id")
    public void update(HmTenant hmTenant){
            HmTenantPO po =  BeanUtil.toBean(hmTenant,HmTenantPO.class);
            hmTenantRepository.updateById(po);
    }

    @CacheEvict(value = "tenant",allEntries = true)
    public void remove(List<Long> ids){
            hmTenantRepository.removeByIds(ids);
    }

    public List<HmTenant> getList(){
        List<HmTenantPO> pos =  hmTenantRepository.list();
        return Convert.toList(HmTenant.class,pos) ;
    }

    public PageBase<HmTenant> getList(int pageNo,int pageSize){
        Page<HmTenantPO> page = new Page<>(pageNo, pageSize);
        IPage<HmTenantPO> resultPage =
                    hmTenantRepository.selectPage(Wrappers.emptyWrapper(),page);
        return PageBase.from(resultPage,HmTenant.class);
    }

    /**
     * 根据域名后缀查询租户信息
     * @param domainSuffix
     * @return
     */
    public HmTenant getByDomain(String domainSuffix){
        QueryWrapper<HmTenantPO> wrapper = new QueryWrapper<>();
        wrapper.eq(HmTenantFields.DOMAIN_SUFFIX,domainSuffix);
        wrapper.eq(HmTenantFields.DELETED, NumberEnum.ZERO.getNum());
        //wrapper.eq(HmTenantFields.STATUS, NumberEnum.ZERO.getNum());
        HmTenantPO po = hmTenantRepository.getOne(wrapper);
        if(po==null) {
            return null;
        }
        return BeanUtil.toBean(po,HmTenant.class);
    }

}